<?php

class SearchController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
    }

    public function ajaxAction() {
    	//Disable view
    	$this->_helper->layout->disableLayout();    //disable layout
    	$this->_helper->viewRenderer->setNoRender(); //suppress auto-rendering
    	
    	//Get post params
    	$request = $this->getRequest();
    	$website = $request->getPost('w');
    	$string = $request->getPost('s');
    	$number = $request->getPost('n'); 
    	
    	//Make a query to the website
    	if ($website == "YT") {
    		$yt = new Zend_Gdata_YouTube();
    		$query = $yt->newVideoQuery();
    		$query->videoQuery = $string;
    		$query->startIndex = $number-9;
    		$query->maxResults = $number;
    		$query->orderBy = 'viewCount';
    		$query->category = 'Music';
    		
    		$responseArray= array();
    		
    		$videoFeed = $yt->getVideoFeed($query);
    		foreach ($videoFeed as $videoEntry) {
    			$video['title'] = $videoEntry->getVideoTitle();
    			$video['id'] = $videoEntry->getVideoId();
				$video['duration'] = $videoEntry->getVideoDuration();
				array_push($responseArray, $video);
    		}
    	}
    	echo json_encode($responseArray);    	
    }

}

